package com.di.base.controller;


import com.di.base.common.dto.AccountRequestDTO;
import com.di.base.common.exception.BusinessException;
import com.di.base.service.AccountTblService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * (AccountTbl)表控制层
 *
 * @author makejava
 * @since 2025-07-08 22:45:15
 */

@Slf4j
@RestController
@RequestMapping("account")
public class AccountTblController {
    @Resource
    private AccountTblService accountTblService;

    @PostMapping("/decrease")
    public void decrease(@RequestBody AccountRequestDTO request) {
        String userId = request.getUserId();
        Double amount = request.getAmount();
        try {
            accountTblService.decrease(userId, amount);
        } catch (BusinessException e) {
            log.error("账户扣减失败 userId={}, amount={}", userId, amount, e);
            throw e; // 继续抛出异常，保持 Saga 识别
        }
    }

    @PostMapping("/increase")
    public void increase(@RequestBody AccountRequestDTO request) {
        String userId = request.getUserId();
        Double amount = request.getAmount();
        log.warn("Saga补偿执行：增加账户余额 userId={}, amount={}", userId, amount);
        try {
            accountTblService.increase(userId, amount);
        } catch (BusinessException e) {
            log.error("账户回滚失败 userId={}, amount={}", userId, amount, e);
            throw e; // 继续抛出异常，保持 Saga 识别
        }
    }
}